package com.yizhuoyan.his.serviceimpl;

import com.yizhuoyan.his.ao.CheckObjectRecordAo;
import com.yizhuoyan.his.dao.*;
import com.yizhuoyan.his.dto.CheckObjectRecordDto;
import com.yizhuoyan.his.entity.*;
import com.yizhuoyan.his.exception.HISException;
import com.yizhuoyan.his.service.doctor.outpatient.InspectionApplicationService;
import com.yizhuoyan.his.servicesupport.DatabaseIdGenerateSupportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.stream.LongStream;

@Service
@Validated
@Slf4j
@Transactional
public class InspectionApplicationServiceImpl implements InspectionApplicationService {

    @Autowired
    CaseCheckRecordDao caseCheckRecordDao;

    @Autowired
    CaseMedicalRecordDao caseMedicalRecordDao;

    @Autowired
    CaseRegistrationRecordDao caseRegistrationRecordDao;

    @Autowired
    HosDepartmentDao departmentDao;

    @Autowired
    HosNodrugDao hosNodrugDao;

    @Autowired
    DatabaseIdGenerateSupportService idGenerateSupportService;

    @Override
    public CheckObjectRecordDto queryCheckProject(Long id) throws Exception {

        //1、验证参数

        //2、执行业务逻辑

        CheckObjectRecordDto checkObjectRecordDto=new CheckObjectRecordDto();
        //获取CaseCheckRecord表中的数据
        CaseCheckRecordEntity caseCheckRecordEntity = caseCheckRecordDao.selectById(id);
        //获取项目名称
        checkObjectRecordDto.setProjectName(caseCheckRecordEntity.getProjectName());
        //从CaseCheckRecord表中获取caseMedicalRecord表中的id
        Long caseMedicalId = caseCheckRecordEntity.getMedicalRecordId();
        //将caseMedicalId传入caseCheckRecordDao中查询
        CaseMedicalRecordEntity caseMedicalRecordEntity = caseMedicalRecordDao.selectById(caseMedicalId);
        //获取检查注意事项
        checkObjectRecordDto.setCarefulDetail(caseMedicalRecordEntity.getCarefulDetail());
        //获取检查意见
        checkObjectRecordDto.setProposalDetail(caseMedicalRecordEntity.getProposalDetail());
        //获得case_registration_record表中的数据caseRegistrationId
        Long caseRegistrationId = caseCheckRecordEntity.getRegistrationRecordId();
        //获得caseRegistrationId中的数据
        CaseRegistrationRecordEntity caseRegistrationRecordEntity = caseRegistrationRecordDao.selectById(caseMedicalId);
        //获取科室id
        Long registerDepartmentId = caseRegistrationRecordEntity.getRegisterDepartmentId();
        //获取HosDepartmentEntity数据
        HosDepartmentEntity hosDepartmentEntity = departmentDao.selectById(registerDepartmentId);
        //获得科室名字
        checkObjectRecordDto.setName(hosDepartmentEntity.getName());
        //获取项目的id
        checkObjectRecordDto.setNondrugExpenseItemId(caseCheckRecordEntity.getNodrugExpenseItemId());
        //获取项目规格
        checkObjectRecordDto.setPosition(caseCheckRecordEntity.getPosition());
        //获取nondrugExpenseItemId的id，对应价格表id
        Long nondrugExpenseItemId = caseCheckRecordEntity.getNodrugExpenseItemId();
        //获取HosNodrugEntity的数据
        HosNodrugEntity hosNodrugEntity = hosNodrugDao.selectById(nondrugExpenseItemId);
        //获取价格
        checkObjectRecordDto.setUnitPrice(hosNodrugEntity.getUnitPrice());

        //3、组装结果
        return checkObjectRecordDto;
    }

    @Override
    public void addCheckProject(CheckObjectRecordAo ao) throws Exception {
            //验证参数
        //执行业务逻辑
        //组装业务结果
        //case_medical_record
        //检查注意事项

        String carefulDetail = ao.getCarefulDetail();
        if(carefulDetail==null||carefulDetail.trim().length()==0){
            log.trace("检查注意事项不能为空");
            throw  new HISException("error","检查注意事项输入不正确");
        }
        //检查建议
         String proposalDetail = ao.getProposalDetail();
        if(proposalDetail==null&&proposalDetail.trim().length()==0){
            log.trace("检查建议不能为空");
            throw  new HISException("error","检查建议输入不正确");
        }
         String projectName = ao.getProjectName();
        if(projectName==null&&projectName.trim().length()==0){
            log.trace("项目名称不能为空");
            throw  new HISException("error","项目名称输入不正确");
        }
         String position = ao.getPosition();
        if(position==null&&position.trim().length()==0){
            log.trace("检查部位不能为空");
            throw  new HISException("error","检查部位输入不正确");
        }
         Long nondrugExpenseItemId = ao.getNondrugExpenseItemId();
        if(nondrugExpenseItemId==null&&nondrugExpenseItemId.toString().length()==0){
            log.trace("检查id不能为空");
            throw  new HISException("error","检查id输入不正确");
        }
        Long medicalRecordId1 = ao.getMedicalRecordId();
        CaseCheckRecordEntity caseCheckRecordEntity=new CaseCheckRecordEntity();
        caseCheckRecordEntity.setId(idGenerateSupportService.nextId());
        caseCheckRecordEntity.setMedicalRecordId(idGenerateSupportService.nextId());
        caseCheckRecordEntity.setRegistrationRecordId(idGenerateSupportService.nextId());
        caseCheckRecordEntity.setCreateTime(LocalDateTime.now());
        //检查
        caseCheckRecordEntity.setRecordType(1);
        caseCheckRecordEntity.setProjectName(projectName);
        caseCheckRecordEntity.setPosition(position);
        caseCheckRecordEntity.setNodrugExpenseItemId(nondrugExpenseItemId);
        caseCheckRecordDao.insert(caseCheckRecordEntity);

        caseCheckRecordEntity.setMedicalRecordId(medicalRecordId1);

        CaseMedicalRecordEntity caseMedicalRecordEntity=new CaseMedicalRecordEntity();
        caseMedicalRecordEntity.setCaseNo("测试");
        caseMedicalRecordEntity.setRegistrationId(idGenerateSupportService.nextId());
        caseMedicalRecordEntity.setId(medicalRecordId1);
        caseMedicalRecordEntity.setCarefulDetail(carefulDetail);
        caseMedicalRecordEntity.setProposalDetail(proposalDetail);
        caseMedicalRecordDao.insert(caseMedicalRecordEntity);
    }

    @Override
    public void removeCheckProject(Long id) throws Exception {
        //判断是否处于暂存状态
        CaseCheckRecordEntity caseCheckRecordEntity = caseCheckRecordDao.selectById(id);
        //得到状态值
        Integer state = caseCheckRecordEntity.getState();
        if(state==1){
            log.trace("状态值为1,处于暂存状态,可以删除");
            caseCheckRecordDao.delete(id);
        }else if(state==2) {
            log.trace("状态值为2,处于已开立状态,不可以删除");
            throw  new HISException("error","处于开立状态,不可以删除项目");
        }else if(state==3){
            log.trace("状态值为3,处于已缴费状态,不可以删除");
            throw  new HISException("error","处于已缴费状态,不可以删除项目");
        }
    }
}
